package ClientsAndHotels.gui;

import jade.wrapper.ControllerException;
import jade.wrapper.StaleProxyException;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

import ClientsAndHotels.GuiAgent;
import ClientsAndHotels.Utils;
import ClientsAndHotels.types.ClientParams;
import ClientsAndHotels.types.HotelParams;
import ClientsAndHotels.types.Room;
import java.util.*;

public class GuiFrame extends javax.swing.JFrame {

    private static final String TITLE = "Hotele";
    private HotelFrame hotelFrame;
    private ClientFrame clientFrame;
    private MediatorFrame mediatorFrame;
    private GuiAgent agent;

    public GuiFrame(GuiAgent agent) {
        initComponents();
        this.agent = agent;
        try {
            agent.createCalendar("calendar");
        } catch (StaleProxyException e) {
            e.printStackTrace();
        }

        hotelFrame = new HotelFrame();
        hotelFrame.setModal(true);

        DefaultTableModel hModel = new DefaultTableModel();
        hModel.addColumn("Nazwa");
        hModel.addColumn("Miasto");
        hModel.addColumn("Liczba pokoi");
        hModel.addColumn("Gwiazdki");

        hotelsTable.setModel(hModel);

        clientFrame = new ClientFrame();
        clientFrame.setModal(true);

        DefaultTableModel cModel = new DefaultTableModel();
        cModel.addColumn("Nazwa");
        cModel.addColumn("Miasto");
        cModel.addColumn("Min gwiazdek");
        cModel.addColumn("Max gwiazdek");
        cModel.addColumn("Liczba osób");
        cModel.addColumn("Cena");
        cModel.addColumn("Zarezerwowany");

        clientsTable.setModel(cModel);

        mediatorFrame = new MediatorFrame();
        mediatorFrame.setModal(true);

        DefaultTableModel mModel = new DefaultTableModel();
        mModel.addColumn("Nazwa");

        mediatorsTable.setModel(mModel);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed"
    // <editor-fold defaultstate="collapsed"
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        hotelsTable = new javax.swing.JTable();
        addHotelButton = new javax.swing.JButton();
        removeHotelButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        clientsTable = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        mediatorsTable = new javax.swing.JTable();
        addClientButton = new javax.swing.JButton();
        removeClientButton = new javax.swing.JButton();
        addMediatorButton = new javax.swing.JButton();
        removeMediatorButton = new javax.swing.JButton();
        testCase1Button = new javax.swing.JButton();
        testCase2Button = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Hotele");

        hotelsTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(hotelsTable);
        hotelsTable.getAccessibleContext().setAccessibleName("");

        addHotelButton.setText("Dodaj");
        addHotelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onAddHotelButtonClick(evt);
            }
        });

        removeHotelButton.setText("Usuń wybrany");
        removeHotelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onRemoveHotelButtonClick(evt);
            }
        });

        jLabel1.setText("Hotele:");

        jLabel2.setText("Klienci:");

        clientsTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4", "Title 5", "Title 6", "Title 7"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(clientsTable);

        jLabel3.setText("Pośrednicy:");

        mediatorsTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane3.setViewportView(mediatorsTable);

        addClientButton.setText("Dodaj");
        addClientButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onAddClientButtonClick(evt);
            }
        });

        removeClientButton.setText("Usuń wybrany");
        removeClientButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onRemoveClientButtonClick(evt);
            }
        });

        addMediatorButton.setText("Dodaj");
        addMediatorButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onAddMediatorButtonClick(evt);
            }
        });

        removeMediatorButton.setText("Usuń wybrany");
        removeMediatorButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onRemoveMediatorButtonClick(evt);
            }
        });

        testCase1Button.setText("Test case 1");
        testCase1Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                testCase1ButtonActionPerformed(evt);
            }
        });

        testCase2Button.setText("Test case 2");
        testCase2Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                testCase2ButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(jLabel3)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 408, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 408, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 408, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(addClientButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(removeClientButton))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(addMediatorButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(removeMediatorButton))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(addHotelButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(removeHotelButton))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addComponent(testCase1Button)
                        .addGap(18, 18, 18)
                        .addComponent(testCase2Button)))
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(5, 5, 5)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addHotelButton)
                    .addComponent(removeHotelButton))
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addClientButton)
                    .addComponent(removeClientButton))
                .addGap(18, 18, 18)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addMediatorButton)
                    .addComponent(removeMediatorButton))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(testCase1Button)
                    .addComponent(testCase2Button))
                .addContainerGap(21, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void testCase2ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_testCase2ButtonActionPerformed

        Random rand = new Random();
        testCase1Button.setEnabled(false);
        testCase2Button.setEnabled(false);
        agent.log("======================= Test case 2 =============================");
        agent.log("Tworzę mediatora.");
        try {
            agent.createMediator("M1");
            ((DefaultTableModel) mediatorsTable.getModel()).addRow(new Object[]{"M1"});
        } catch (StaleProxyException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this,
                    String.format("Wystąpił błąd: %s", e.getMessage()),
                    "Wyjątek", JOptionPane.ERROR_MESSAGE);
        }

        int num = rand.nextInt(20) + 10;
        for (int i = 0; i < num; ++i) {
            agent.log("Tworzę hotel " + i + ".");
            try {
                int num2 = rand.nextInt(10) + 1;
                ArrayList<Room> rooms = new ArrayList<>();
                int roomFor = rand.nextInt(4) + 1;
                double price = rand.nextDouble() * 200;
                for (int i2 = 0; i2 < num2; ++i2) {
                    rooms.add(new Room(i2, roomFor, price));
                }
                agent.createHotel(new HotelParams("H" + i, "Warszawa", roomFor, rooms));
                ((DefaultTableModel) hotelsTable.getModel()).addRow(new Object[]{
                            "H" + i, "Warszawa", rooms.size(), roomFor});
            } catch (StaleProxyException e) {
                e.printStackTrace();
                JOptionPane.showMessageDialog(this,
                        String.format("Wystąpił błąd: %s", e.getMessage()),
                        "Wyjątek", JOptionPane.ERROR_MESSAGE);
            }
        }

        num = rand.nextInt(100) + 40;
        for (int i = 0; i < num; ++i) {
            agent.log("Tworzę klienta " + num + ".");
            try {
                int roomFor = rand.nextInt(4) + 1;
                double price = rand.nextDouble() * 210;
                int days = rand.nextInt(7) + 1;
                Date date = parseDate("20-06-2012");
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                calendar.add(Calendar.DAY_OF_MONTH, rand.nextInt(6));
                agent.createClient(new ClientParams("K" + i, "Warszawa", 1, 5, roomFor,
                        price, calendar.getTime(), days));
                ((DefaultTableModel) clientsTable.getModel()).addRow(new Object[]{
                            "K" + i, "Warszawa", 1, 5, roomFor, price, "NIE"});
            } catch (StaleProxyException e) {
                e.printStackTrace();
                JOptionPane.showMessageDialog(this,
                        String.format("Wystąpił błąd: %s", e.getMessage()),
                        "Wyjątek", JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_testCase2ButtonActionPerformed

    private void onAddHotelButtonClick(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_onAddHotelButtonClick
        hotelFrame.reset();
        hotelFrame.setVisible(true);
        if (!hotelFrame.isCancelled()) {
            try {
                agent.createHotel(hotelFrame.getHotel());
                ((DefaultTableModel) hotelsTable.getModel()).addRow(new Object[]{hotelFrame.getHotelName(),
                            hotelFrame.getHotelCity(),
                            hotelFrame.getHotelRooms(),
                            hotelFrame.getHotelStars()});
            } catch (StaleProxyException e) {
                e.printStackTrace();
                JOptionPane.showMessageDialog(this,
                        String.format("Wystąpił błąd: %s", e.getMessage()),
                        "Wyjątek", JOptionPane.ERROR_MESSAGE);
            }
        }
    }// GEN-LAST:event_onAddHotelButtonClick

    private void onRemoveHotelButtonClick(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_onRemoveHotelButtonClick
        int selectedRow = hotelsTable.getSelectedRow();
        if (selectedRow == -1) {
            return;
        }
        String name = (String) hotelsTable.getValueAt(selectedRow, 0);
        try {
            agent.killAgent(name);
            ((DefaultTableModel) hotelsTable.getModel()).removeRow(selectedRow);
        } catch (ControllerException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this,
                    String.format("Wystąpił błąd: %s", e.getMessage()),
                    "Wyjątek", JOptionPane.ERROR_MESSAGE);
        }
    }// GEN-LAST:event_onRemoveHotelButtonClick

    private void onAddClientButtonClick(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_onAddClientButtonClick
        clientFrame.reset();
        clientFrame.setVisible(true);
        if (!clientFrame.isCancelled()) {
            try {
                agent.createClient(clientFrame.getClient());
                ((DefaultTableModel) clientsTable.getModel()).addRow(new Object[]{clientFrame.getClientName(),
                            clientFrame.getClientCity(),
                            clientFrame.getClientMinStars(),
                            clientFrame.getClientMaxStars(),
                            clientFrame.getClientRoomFor(),
                            clientFrame.getClientPrice(),
                            "NIE"});
            } catch (StaleProxyException e) {
                e.printStackTrace();
                JOptionPane.showMessageDialog(this,
                        String.format("Wystąpił błąd: %s", e.getMessage()),
                        "Wyjątek", JOptionPane.ERROR_MESSAGE);
            }
        }
    }// GEN-LAST:event_onAddClientButtonClick

    private void onRemoveClientButtonClick(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_onRemoveClientButtonClick
        int selectedRow = clientsTable.getSelectedRow();
        if (selectedRow == -1) {
            return;
        }
        String name = (String) clientsTable.getValueAt(selectedRow, 0);
        try {
            agent.killAgent(name);
            ((DefaultTableModel) clientsTable.getModel()).removeRow(selectedRow);
        } catch (ControllerException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this,
                    String.format("Wystąpił błąd: %s", e.getMessage()),
                    "Wyjątek", JOptionPane.ERROR_MESSAGE);
        }
    }// GEN-LAST:event_onRemoveClientButtonClick

    private void onAddMediatorButtonClick(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_onAddMediatorButtonClick
        mediatorFrame.reset();
        mediatorFrame.setVisible(true);
        if (!mediatorFrame.isCancelled()) {
            try {
                agent.createMediator(mediatorFrame.getMediatorName());
                ((DefaultTableModel) mediatorsTable.getModel()).addRow(new Object[]{mediatorFrame.getMediatorName()});
            } catch (StaleProxyException e) {
                e.printStackTrace();
                JOptionPane.showMessageDialog(this,
                        String.format("Wystąpił błąd: %s", e.getMessage()),
                        "Wyjątek", JOptionPane.ERROR_MESSAGE);
            }
        }
    }// GEN-LAST:event_onAddMediatorButtonClick

    private void onRemoveMediatorButtonClick(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_onRemoveMediatorButtonClick
        int selectedRow = mediatorsTable.getSelectedRow();
        if (selectedRow == -1) {
            return;
        }
        String name = (String) mediatorsTable.getValueAt(selectedRow, 0);
        try {
            agent.killAgent(name);
            ((DefaultTableModel) mediatorsTable.getModel()).removeRow(selectedRow);
        } catch (ControllerException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this,
                    String.format("Wystąpił błąd: %s", e.getMessage()),
                    "Wyjątek", JOptionPane.ERROR_MESSAGE);
        }
    }// GEN-LAST:event_onRemoveMediatorButtonClick

    private void testCase1ButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_testCase1ButtonActionPerformed

        testCase1Button.setEnabled(false);
        testCase2Button.setEnabled(false);
        agent.log("======================= Test case 1 =============================");
        agent.log("Tworzę mediatora.");
        try {
            agent.createMediator("M1");
            ((DefaultTableModel) mediatorsTable.getModel()).addRow(new Object[]{"M1"});
        } catch (StaleProxyException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this,
                    String.format("Wystąpił błąd: %s", e.getMessage()),
                    "Wyjątek", JOptionPane.ERROR_MESSAGE);
        }

        agent.log("Tworzę hotel 1.");
        try {
            ArrayList<Room> rooms = new ArrayList<>(Arrays.asList(new Room(0,
                    2, 120.00), new Room(1, 2, 150.00), new Room(2, 2, 140.00),
                    new Room(3, 1, 150.00), new Room(4, 1, 100.00), new Room(5,
                    2, 190.00), new Room(6, 4, 250.00), new Room(7, 2,
                    110.00), new Room(8, 3, 200.00)));
            agent.createHotel(new HotelParams("H1", "Warszawa", 2, rooms));
            ((DefaultTableModel) hotelsTable.getModel()).addRow(new Object[]{
                        "H1", "Warszawa", rooms.size(), 2});
        } catch (StaleProxyException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this,
                    String.format("Wystąpił błąd: %s", e.getMessage()),
                    "Wyjątek", JOptionPane.ERROR_MESSAGE);
        }

        agent.log("Tworzę hotel 2.");
        try {
            ArrayList<Room> rooms = new ArrayList<>(Arrays.asList(new Room(0,
                    1, 90.00), new Room(1, 3, 120.00), new Room(2, 4, 220.00),
                    new Room(3, 2, 150.00), new Room(4, 3, 180.00), new Room(5,
                    1, 90.00), new Room(6, 2, 110.00)));
            agent.createHotel(new HotelParams("H2", "Warszawa", 4, rooms));
            ((DefaultTableModel) hotelsTable.getModel()).addRow(new Object[]{
                        "H2", "Warszawa", rooms.size(), 4});
        } catch (StaleProxyException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this,
                    String.format("Wystąpił błąd: %s", e.getMessage()),
                    "Wyjątek", JOptionPane.ERROR_MESSAGE);
        }

        agent.log("Tworzę hotel 3.");
        try {
            ArrayList<Room> rooms = new ArrayList<>(Arrays.asList(new Room(0,
                    2, 100.00), new Room(1, 2, 120.00), new Room(2, 2, 120.00),
                    new Room(3, 3, 180.00), new Room(4, 4, 280.00), new Room(5,
                    2, 90.00), new Room(6, 2, 110.00), new Room(7, 2,
                    90.00), new Room(8, 3, 150.00), new Room(9, 4,
                    290.00), new Room(10, 4, 250.00)));
            agent.createHotel(new HotelParams("H3", "Warszawa", 3, rooms));
            ((DefaultTableModel) hotelsTable.getModel()).addRow(new Object[]{
                        "H3", "Warszawa", rooms.size(), 3});
        } catch (StaleProxyException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this,
                    String.format("Wystąpił błąd: %s", e.getMessage()),
                    "Wyjątek", JOptionPane.ERROR_MESSAGE);
        }

        agent.log("Tworzę klienta 1.");
        try {
            agent.createClient(new ClientParams("K1", "Warszawa", 1, 5, 2,
                    100.00, parseDate("20-06-2012"), 1));
            ((DefaultTableModel) clientsTable.getModel()).addRow(new Object[]{
                        "K1", "Warszawa", 1, 5, 2, 100.00, "NIE"});
        } catch (StaleProxyException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this,
                    String.format("Wystąpił błąd: %s", e.getMessage()),
                    "Wyjątek", JOptionPane.ERROR_MESSAGE);
        }

        agent.log("Tworzę klienta 2.");
        try {
            agent.createClient(new ClientParams("K2", "Warszawa", 1, 5, 2,
                    150.00, parseDate("20-06-2012"), 1));
            ((DefaultTableModel) clientsTable.getModel()).addRow(new Object[]{
                        "K2", "Warszawa", 1, 5, 2, 150.00, "NIE"});
        } catch (StaleProxyException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this,
                    String.format("Wystąpił błąd: %s", e.getMessage()),
                    "Wyjątek", JOptionPane.ERROR_MESSAGE);
        }

        agent.log("Tworzę klienta 3.");
        try {
            agent.createClient(new ClientParams("K3", "Warszawa", 1, 5, 4,
                    250.00, parseDate("20-06-2012"), 1));
            ((DefaultTableModel) clientsTable.getModel()).addRow(new Object[]{
                        "K3", "Warszawa", 1, 5, 4, 250.00, "NIE"});
        } catch (StaleProxyException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this,
                    String.format("Wystąpił błąd: %s", e.getMessage()),
                    "Wyjątek", JOptionPane.ERROR_MESSAGE);
        }

        agent.log("Tworzę klienta 4.");
        try {
            agent.createClient(new ClientParams("K4", "Warszawa", 1, 5, 3,
                    150.00, parseDate("20-06-2012"), 1));
            ((DefaultTableModel) clientsTable.getModel()).addRow(new Object[]{
                        "K4", "Warszawa", 1, 5, 3, 150.00, "NIE"});
        } catch (StaleProxyException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this,
                    String.format("Wystąpił błąd: %s", e.getMessage()),
                    "Wyjątek", JOptionPane.ERROR_MESSAGE);
        }

        agent.log("Tworzę klienta 5.");
        try {
            agent.createClient(new ClientParams("K5", "Warszawa", 1, 5, 2,
                    250.00, parseDate("20-06-2012"), 1));
            ((DefaultTableModel) clientsTable.getModel()).addRow(new Object[]{
                        "K5", "Warszawa", 1, 5, 2, 250.00, "NIE"});
        } catch (StaleProxyException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this,
                    String.format("Wystąpił błąd: %s", e.getMessage()),
                    "Wyjątek", JOptionPane.ERROR_MESSAGE);
        }

        agent.log("Tworzę klienta 6.");
        try {
            agent.createClient(new ClientParams("K6", "Warszawa", 1, 5, 2,
                    250.00, parseDate("20-06-2012"), 1));
            ((DefaultTableModel) clientsTable.getModel()).addRow(new Object[]{
                        "K6", "Warszawa", 1, 5, 2, 250.00, "NIE"});
        } catch (StaleProxyException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this,
                    String.format("Wystąpił błąd: %s", e.getMessage()),
                    "Wyjątek", JOptionPane.ERROR_MESSAGE);
        }

        agent.log("Tworzę klienta 7.");
        try {
            agent.createClient(new ClientParams("K7", "Warszawa", 1, 5, 2,
                    150.00, parseDate("20-06-2012"), 1));
            ((DefaultTableModel) clientsTable.getModel()).addRow(new Object[]{
                        "K7", "Warszawa", 1, 5, 2, 150.00, "NIE"});
        } catch (StaleProxyException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this,
                    String.format("Wystąpił błąd: %s", e.getMessage()),
                    "Wyjątek", JOptionPane.ERROR_MESSAGE);
        }

        agent.log("Tworzę klienta 8.");
        try {
            agent.createClient(new ClientParams("K8", "Warszawa", 1, 5, 3,
                    220.00, parseDate("20-06-2012"), 1));
            ((DefaultTableModel) clientsTable.getModel()).addRow(new Object[]{
                        "K8", "Warszawa", 1, 5, 3, 220.00, "NIE"});
        } catch (StaleProxyException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this,
                    String.format("Wystąpił błąd: %s", e.getMessage()),
                    "Wyjątek", JOptionPane.ERROR_MESSAGE);
        }

        agent.log("Tworzę klienta 9.");
        try {
            agent.createClient(new ClientParams("K9", "Warszawa", 1, 5, 3,
                    250.00, parseDate("20-06-2012"), 1));
            ((DefaultTableModel) clientsTable.getModel()).addRow(new Object[]{
                        "K9", "Warszawa", 1, 5, 3, 250.00, "NIE"});
        } catch (StaleProxyException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this,
                    String.format("Wystąpił błąd: %s", e.getMessage()),
                    "Wyjątek", JOptionPane.ERROR_MESSAGE);
        }

        agent.log("Tworzę klienta 10.");
        try {
            agent.createClient(new ClientParams("K10", "Warszawa", 1, 5, 2,
                    120.00, parseDate("20-06-2012"), 1));
            ((DefaultTableModel) clientsTable.getModel()).addRow(new Object[]{
                        "K10", "Warszawa", 1, 5, 2, 120.00, "NIE"});
        } catch (StaleProxyException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this,
                    String.format("Wystąpił błąd: %s", e.getMessage()),
                    "Wyjątek", JOptionPane.ERROR_MESSAGE);
        }

        agent.log("Tworzę klienta 11.");
        try {
            agent.createClient(new ClientParams("K11", "Warszawa", 1, 5, 2,
                    100.00, parseDate("20-06-2012"), 1));
            ((DefaultTableModel) clientsTable.getModel()).addRow(new Object[]{
                        "K11", "Warszawa", 1, 5, 2, 100.00, "NIE"});
        } catch (StaleProxyException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this,
                    String.format("Wystąpił błąd: %s", e.getMessage()),
                    "Wyjątek", JOptionPane.ERROR_MESSAGE);
        }

        agent.log("Tworzę klienta 12.");
        try {
            agent.createClient(new ClientParams("K12", "Warszawa", 1, 5, 3,
                    170.00, parseDate("20-06-2012"), 1));
            ((DefaultTableModel) clientsTable.getModel()).addRow(new Object[]{
                        "K12", "Warszawa", 1, 5, 3, 170.00, "NIE"});
        } catch (StaleProxyException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this,
                    String.format("Wystąpił błąd: %s", e.getMessage()),
                    "Wyjątek", JOptionPane.ERROR_MESSAGE);
        }

        agent.log("Tworzę klienta 13.");
        try {
            agent.createClient(new ClientParams("K13", "Warszawa", 1, 5, 4,
                    310.00, parseDate("20-06-2012"), 1));
            ((DefaultTableModel) clientsTable.getModel()).addRow(new Object[]{
                        "K13", "Warszawa", 1, 5, 4, 310.00, "NIE"});
        } catch (StaleProxyException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this,
                    String.format("Wystąpił błąd: %s", e.getMessage()),
                    "Wyjątek", JOptionPane.ERROR_MESSAGE);
        }
    }// GEN-LAST:event_testCase1ButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        // <editor-fold defaultstate="collapsed"
        // desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase
         * /tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GuiFrame.class.getName()).log(
                    java.util.logging.Level.SEVERE, null, ex);
        }
        // </editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                startGui(null);
            }
        });
    }

    public static GuiFrame startGui(GuiAgent agent) {
        final GuiFrame gui = new GuiFrame(agent);
        gui.setLocationByPlatform(true);
        gui.setVisible(true);
        return gui;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addClientButton;
    private javax.swing.JButton addHotelButton;
    private javax.swing.JButton addMediatorButton;
    private javax.swing.JTable clientsTable;
    private javax.swing.JTable hotelsTable;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable mediatorsTable;
    private javax.swing.JButton removeClientButton;
    private javax.swing.JButton removeHotelButton;
    private javax.swing.JButton removeMediatorButton;
    private javax.swing.JButton testCase1Button;
    private javax.swing.JButton testCase2Button;
    // End of variables declaration//GEN-END:variables

    public void setDate(Date date) {
        setTitle(String.format("%s (%s)", TITLE, new SimpleDateFormat(
                "dd-MM-yyyy").format(date)));
    }

    private static Date parseDate(String date) {
        try {
            return Utils.createDate(new SimpleDateFormat("dd-MM-yyyy").parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public synchronized void removeClient(String agentName) {
        DefaultTableModel model = (DefaultTableModel) clientsTable.getModel();
        for (int i = model.getRowCount() - 1; i >= 0; --i) {
            if (model.getValueAt(i, 0).equals(agentName)) {
                model.removeRow(i);
                return;
            }
        }

    }

    public synchronized void removeMediator(String agentName) {
        DefaultTableModel model = (DefaultTableModel) mediatorsTable.getModel();
        for (int i = model.getRowCount() - 1; i >= 0; --i) {
            if (model.getValueAt(i, 0).equals(agentName)) {
                model.removeRow(i);
                return;
            }
        }
    }

    public synchronized void removeHotel(String agentName) {
        DefaultTableModel model = (DefaultTableModel) hotelsTable.getModel();
        for (int i = model.getRowCount() - 1; i >= 0; --i) {
            if (model.getValueAt(i, 0).equals(agentName)) {
                model.removeRow(i);
                return;
            }
        }
    }

    public synchronized void markClientDone(String agentName) {
        DefaultTableModel model = (DefaultTableModel) clientsTable.getModel();
        for (int i = model.getRowCount() - 1; i >= 0; --i) {
            if (model.getValueAt(i, 0).equals(agentName)) {
                model.setValueAt("TAK", i, 6);
                return;
            }
        }
    }
}
